/**
 * Created by jean.h.ma on 01/04/2018.
 */

import React, {PureComponent} from 'react'
import Master from '../../components/Master'
import Base from '@codefree/badger/app/components/Base'
//import HelloWorld from '../../components/HelloWorld'
import axios from 'axios'
import {DataTable, DataTableWithPagination} from 'react-component-data-table'
import {Link} from 'react-router-dom'
import PropTypes from 'prop-types'
export default class Schedule extends Base {
	static contextTypes={
		router: PropTypes.object
	};
	constructor(props) {
		super(props);
		this.state = {
			id:null,
			major_name: null,
			schedule_start: null,
			schedule_end:null,
			test_dline:null,
			prac_dline:null,
			dataSource: [],
			total: 0,
			pageIndex: 0,
			pageSize: 2
		};
		this.dataTableInstance = null;
		this.columnDefine = [{
			name: "班级名",
			render: rowData => rowData['major_name']
		}, {
			name: "考试时间",
			render: rowData => rowData['schedule_start']+"到"+rowData['schedule_end']
		}, {
			name: "练习有效期",
			render: rowData => rowData['prac_dline']
		},{
			name: "测验有效期",
			render: rowData => rowData['test_dline']
		},{
			name: "操作",
			render: rowData => {
				return (
					<button type='button' className="pure-button pure-button-primary" href="javascript:void(0)" onClick={() => {
						this.context.router.history.push({
							pathname:"/test/scheduleList",
							search:"id="+rowData['id'],
						})
					}}
					
					>修改</button>
				);
			}
		}];
	}





	async query(filter) {
		const {data} = await axios.get(`http://127.0.0.1:3000/schedule/query`, filter);
		if (data.code === 0) {
			this.updateState({
				dataSource: {$set: data.data.list},
				total: {$set: data.data.total},
			});
		}
		else {
			alert(data.message);
		}
	}

	render() {
		return (
			<Master>
				<DataTableWithPagination columns={this.columnDefine}
										 ref={ref => this.dataTableInstance = ref}
										 pageIndex={this.state.pageIndex}
										 pageSize={this.state.pageSize}
										 total={this.state.total}
										 onPageChange={filter => {
								
											 this.query({
												 pageIndex: filter.pageIndex,
												 pageSize: filter.pageSize
											 });
										 }}
										 renderNextPage={()=>{
											 return <span>下一页</span>;
										 }}
										 renderPrevPage={()=>{
											 return <span>上一页</span>;
										 }}
										 dataTableClassName="pure-table"
										 paginationClassName="pagination"
										 dataSource={this.state.dataSource}/>
			</Master>
		);
	}

	//componentWillUnmount
	componentDidMount() {
		super.componentDidMount();
		this.query({
			pageIndex: this.state.pageIndex,
			pageSize: this.state.pageSize
		});
	}
}